package AgentTravel.Behaviours;

import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

import org.joda.time.LocalDate;

import AgentTravel.LogicObjects.DayPlan;
import AgentTravel.LogicObjects.User;
import AgentTravel.LogicObjects.UserInfoForServer;
import AgentTravel.Messages.NewDayPlanMessage;
import AgentTravel.Messages.PlanMyTripMessage;
import AgentTravel.Messages.ServerConfirmationInfo;
import AgentTravel.gui.GuiAgent;

import jade.core.AID;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

// Zachowanie do odbierania wiadomosci przez hosta
public class UserAuthorizationBehaviour extends CyclicBehaviour
{
	private AID serverName;

	private GuiAgent agent;
	
	public UserAuthorizationBehaviour(AID serverName, GuiAgent agent)
	{
		this.serverName = serverName;
		this.agent = agent;
	}
	
	@Override
	public void action() 
	{
		MessageTemplate mt = MessageTemplate.MatchSender(serverName);
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null) 
		{
			switch (msg.getPerformative()) {
			case ACLMessage.CONFIRM: // registration succeded
				JOptionPane.showMessageDialog(null, "CONFIRM",
	                       "UserAuthorizationBehaviour", JOptionPane.INFORMATION_MESSAGE);
				handleServerConfirmationInfo(msg);
				break;
			case ACLMessage.DISCONFIRM: // registration failed
				JOptionPane.showMessageDialog(null, "DISCONFIRM",
	                       "UserAuthorizationBehaviour", JOptionPane.INFORMATION_MESSAGE);
				break;
			case ACLMessage.ACCEPT_PROPOSAL: // logIn
				JOptionPane.showMessageDialog(null, "ACCEPT_PROPOSAL",
	                       "UserAuthorizationBehaviour", JOptionPane.INFORMATION_MESSAGE);
				handleServerConfirmationInfo(msg);
				break;
			case ACLMessage.REJECT_PROPOSAL: // logIn
				JOptionPane.showMessageDialog(null, "REJECT_PROPOSAL",
	                       "UserAuthorizationBehaviour", JOptionPane.INFORMATION_MESSAGE);
				break;
			case ACLMessage.INFORM_REF:
				System.out.println("GUI: updateCalendarFromServer");
				handleCalendarUpdate(msg);
				break;
			default:
				break;
			}
		}
		else 
		{
			block();
		}	
	}
	
	void handleCalendarUpdate(ACLMessage msg)
	{
		try {
			NewDayPlanMessage info = (NewDayPlanMessage) msg.getContentObject();
			agent.setCalendarDayPlan(info.day, info.plan);
		} catch (UnreadableException e) {
			JOptionPane.showMessageDialog(null, "Senior! Una problema, couldn't read NewDayPlanMessage!\n"
					+ e.getLocalizedMessage(),
                  "handleServerConfirmationInfo", JOptionPane.ERROR_MESSAGE);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void handleServerConfirmationInfo(ACLMessage msg)
	{
		try {
			ServerConfirmationInfo info = (ServerConfirmationInfo) msg.getContentObject();
			
			agent.setUser(info.yourUserInfo);
			agent.setVisitor(new AID(info.yourVisitorAgent, true));
			agent.setHost(new AID(info.yourHostAgent, true));
		} catch (UnreadableException e) {
			JOptionPane.showMessageDialog(null, "Senior! Una problema, couldn't read user info!",
                    "handleServerConfirmationInfo", JOptionPane.ERROR_MESSAGE);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
} 